const bcrypt = require('bcryptjs')

module.exports = (sequelize, DataTypes) => {
    const User = sequelize.define(
        'User',
        {
            id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                autoIncrement: true,
                primaryKey: true
            },
            username: {
                type: DataTypes.STRING(20),
                allowNull: false,
                unique: true,
                validate: {
                    notNull: {
                        msg: '用户名不能为空'
                    },
                    len: {
                        args: [5, 20],
                        msg: '用户名长度为 5 - 20'
                    }
                },
                comment: '用户名'
            },
            name: {
                type: DataTypes.STRING(10),
                allowNull: true,
                validate: {
                    len: {
                        args: [2, 10],
                        msg: '姓名长度为 2 - 10'
                    }
                },
                comment: '名字'
            },
            password: {
                type: DataTypes.STRING,
                allowNull: true,
                defaultValue: '',
                set(v) {
                    this.setDataValue('password', bcrypt.hashSync(v, bcrypt.genSaltSync(12)))
                },
                comment: '密码'
            },
            role_id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '权限不能为空'
                    }
                },
                references: {
                    model: 'Role',
                    key: 'id'
                },
                comment: '所属权限'
            },
            store_id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '分店不能为空'
                    }
                },
                references: {
                    model: 'Store',
                    key: 'id'
                },
                comment: '所属分店'
            },
            enable: {
                type: DataTypes.BOOLEAN,
                allowNull: true,
                comment: '是否启用'
            },
            tel: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: true,
                validate: {
                    is: {
                        args: /^1\d{10}$/,
                        msg: '手机号码必须为11位数字'
                    }
                },
                comment: '手机号码'
            }
        },
        {
            tableName: 'user'
        }
    )
    // 添加实例级别方法
    User.prototype.comparePassword = function(password) {
        return bcrypt.compareSync(password, this.password)
    }

    User.associate = function(models) {
        models.User.belongsTo(models.Store, { foreignKey: 'store_id', targetKey: 'id' })
        models.User.belongsTo(models.Role, { foreignKey: 'role_id', targetKey: 'id' })
    }

    return User
}
